package com.cpi.cornpp.costelement.action;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.form.CostElementForm;
import com.cpi.cornpp.costelement.services.CostElementServices;
import com.cpi.cornpp.costelement.view.CostElementView;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportChargeView;
import com.cpi.cornpp.transports.view.TransportView;

public class ShowEditCostElementAction extends Action {
	
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws CostElementException{
		String messageError = null;
		CostElementForm costElementForm = null;
		CostElementView costElementView = null;
		TransportView transportView = null;
		TransportChargeView transportChargeView = null;
		TransportService transportService = null;
		CostElementServices costElementService = null;
		List transportsView = null;
		List transportChargesView = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		messageResources = getResources(request);
		
		try{
			
			if (form != null){
				costElementForm = (CostElementForm) form;
				costElementView = (CostElementView) session.getAttribute("costElementView");
					
				if ((costElementView == null) && (costElementForm.getCostElementView().getCostElementID() != -1)){
					costElementService = new CostElementServices();
					
					costElementView = costElementService.getCostElementById(costElementForm.getCostElementView().getCostElementID());
										
					if (costElementView != null){
						transportService = new TransportService();
						transportChargesView = transportService.getTransportChargesByCostElement(costElementView.getCostElementID());
						transportsView = transportService.getTransports();
						
						if (CollectionUtils.isNotEmpty(transportChargesView)){
							
							for (Iterator itrTransportCharge = transportChargesView.iterator(); itrTransportCharge.hasNext(); ){
								transportChargeView = (TransportChargeView) itrTransportCharge.next();
								
								for (Iterator itrTransport = transportsView.iterator(); itrTransport.hasNext(); ){
									transportView = (TransportView) itrTransport.next();
									
									if (transportChargeView.getTransportChargeTransportId() == transportView.getTransportIdentifier()){
										transportView.setTransportCost(transportChargeView.getTransportChargeAmount());
										costElementView.getTransports().add(transportView);
										break;
									}
								}
							}
						}
						
						session.removeAttribute("deletedCostElementTransport");
						session.removeAttribute("addedCostElementsTransport");
						
					} else {
						CostElementException cee = new CostElementException("El transporte seleccionado no existe", CostElementException.LAYER_ACTION, CostElementException.ACTION_SELECT);
						
						throw cee;
					}			
				}
				
				session.setAttribute("costElementView", costElementView);
				request.setAttribute("costElementView", costElementView);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(CostElementException cee){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), cee.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, cee);
				form.reset(mapping, request);
				throw cee;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
			
		} catch(Exception e){
			CostElementException tre = new CostElementException(e , CostElementException.LAYER_ACTION, CostElementException.ACTION_INSERT);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		}
		
		return forward;
	}
	
}